import { Component, OnInit } from '@angular/core';
import { Singlepage } from '../../model/singlepage';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Router, ActivatedRoute } from '@angular/router';
import { SinglepageService } from '../../service/singlepage.service';
import { ConfigService } from '../../service/config.service';

@Component({
  selector: 'app-editpage',
  templateUrl: './editpage.component.html',
  styleUrls: ['./editpage.component.css']
})
export class EditpageComponent implements OnInit {
  singlepage:Singlepage;
  parm:string;
  selectedFile: string;
  constructor(
    private http:HttpClient,
    private router:Router,
    private routerinfo:ActivatedRoute,
    private configservice:ConfigService,//图片问题解决
    private singlepageservice:SinglepageService
  ) {
    this.singlepage=new Singlepage();
    this.parm=this.routerinfo.snapshot.params['id'];
   }

  ngOnInit() {
    if(this.parm=='add'){
      
    }
    else{
      this.getSinglepage(this.parm);
    }
  }

  save(){
    console.dir(this.singlepage);
    if(this.parm=='add'){
      this.addSinglepage();
    }
    else{
      this.updateSinglepage();
    }
       // this.singlepageservice.addSinglepage(this.singlepage)
    // .then((flag:boolean)=>{
    //   if(this.parm=='add'){
    //     this.addSinglepage();
    //   }
    //   else{
    //     this.updateSinglepage();
    //   }
    // })

  }

  onFileChanged(event){
    this.selectedFile=event.target.files[0];

  }

  //上传文件
  onUpload(){
    const uploadData=new FormData();
    uploadData.append('file',this.selectedFile);
    //this.http.post('https://39.108.155.242/manage/fileUpload',uploadData).subscribe(alert(data);
    //图片问题解决this.http.post('https://localhost/manage/fileUpload',uploadData).subscribe((data:any)=>{
    this.http.post(this.configservice.host+'/manage/fileUpload',uploadData).subscribe((data:any)=>{
        // console.log(JSON.stringify(data));
        //let host="https://39.108.155.242/public/";
        //let host="https://localhost/public/";  图片问题解决
        //this.singlepage.img=host+data.pic;   图片问题解决
        this.singlepage.img=data.pic;  //图片问题解决
      },(err:HttpErrorResponse)=>{
        console.log(err.message);
      }
    )
  }

  private addSinglepage(){
    this.singlepageservice.addSinglepage(this.singlepage)
     .then((flag:boolean)=>{
       if(flag==true){
        this.goBack(); //alert('成功');
       }else{
         alert('失败');
       }

     })
  }

  goBack(){
    this.router.navigate(['detailpage']);
  }

  private updateSinglepage(){
    this.singlepageservice.updateSinglepage(this.singlepage)
    .then((flag:boolean)=>{
      if(flag==true){
        this.goBack();
      }
      else{
        alert('保存异常，请刷新后重试');
      }
    })

  }

  private getSinglepage(id:string){
    this.singlepageservice.getSinglepage(id)
    .then((data:Singlepage)=>{
      if(data){
        this.singlepage=data;
      }
      else{
        alert("通讯异常，请刷新后重试");
      }
    })
  }
  

}